﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Diagnostics;
using MySql.Data.MySqlClient;
using System.Threading;

namespace MySqlAutoInstall
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void 安装mysql_Click(object sender, EventArgs e)
        {
            string appPath = Application.StartupPath + "\\MySQL Server 5.5\\";
            if (!Directory.Exists(appPath)) { MessageBox.Show("先确保存在mysql文件！"); return; }

            //1.修改my.ini配置  为防止本机已装mysql，特修改my.ini中端口号为3307
            string iniFile = File.ReadAllText(appPath + "my.ini");
            iniFile = iniFile.Replace("%BaseDir%", Application.StartupPath.Replace("\\","/")); //%BaseDir%为my.ini中自定义的目录参数
            File.WriteAllText(appPath + "my.ini", iniFile);

            txtmsg.Text += "创建win服务……\r\n";
            //2.创建win服务
            string info1 = CommandHelper.Execute(appPath + "bin\\mysqld.exe", " install MySQL3 --defaults-file=\"" + appPath + "my.ini\"", 0);

            txtmsg.Text += info1+"\r\n启动服务……\r\n";
            Thread.Sleep(5000);
            //3.启动服务
            System.ServiceProcess.ServiceController server = new System.ServiceProcess.ServiceController("MySQL3");
            if (server.Status != System.ServiceProcess.ServiceControllerStatus.Running)
            {
                server.Start();
                txtmsg.Text +="正使用System.ServiceProcess.ServiceController启动服务";
            }
            Thread.Sleep(5000);
            //string info=CommandHelper.Execute("net start MySQL3", 0);

            //txtmsg.Text +=info+ "\r\n完成\r\n";

            MySqlConnection con = new MySqlConnection("Data Source='localhost';Port='3307';Database='';User Id='root';Password='';");
            try
            {
                con.Open();
                txtmsg.Text += "连接成功！";
            }
            catch(Exception ex)
            {
                txtmsg.Text += "连接失败！"+ex.Message;
            }
        }

        private void 初始化数据库_Click(object sender, EventArgs e)
        {
            string mysqlcon = "Data Source='localhost';Port='3307';Database='{0}';User Id='root';Password='';";
            MySqlConnection conn = new MySqlConnection(string.Format(mysqlcon,""));
            FileInfo file = new FileInfo(Application.StartupPath + "\\DBInit\\yourDB.sql");  //filename是sql脚本文件路径。
            string sql = file.OpenText().ReadToEnd();
            MySqlScript script = new MySqlScript(conn);
            script.Query = sql;
            int count = script.Execute();

            txtmsg.Text = "数据库初始化完成！\r\n";

            try
            {
                MySqlConnection con2 = new MySqlConnection(string.Format(mysqlcon, "yourDB"));
                con2.Open();
                
                MySqlCommand dbcom = new MySqlCommand("select count(*) from image", con2);
                dbcom.ExecuteScalar();
                txtmsg.Text += "数据库创建OK！";
            }
            catch(Exception ex2)
            {
                txtmsg.Text += "数据库创建失败！"+ex2.Message;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string appPath = Application.StartupPath + "\\MySQL Server 5.5\\";
            txtmsg.Text = "点击按钮前，请先保证以下目录下有mysql5.5文件：\r\n"+appPath;
        }

       
    }
}
